#ifndef CALIBRATION_H
#define CALIBRATION_H

#include <QWizard>

#include <QGridLayout>
#include <QInputDialog>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QGroupBox>
#include <QRadioButton>
#include <QProgressBar>
#include <QPushButton>
#include <QSplashScreen>
#include <QMap>
#include <cmath>
#include "Control/config.h"


#define CALIBRATIONSAMPLES 5

//class QCheckBox;
//class QGroupBox;
//class QLabel;
//class QLineEdit;
//class QRadioButton;


class IntroCalPage : public QWizardPage
{
    Q_OBJECT

public:
    IntroCalPage(QWidget *parent = 0);

private:
    QLabel *label;
};
/** **************************************************************** */
class CalibrationPage : public QWizardPage
{
    Q_OBJECT

public:
    CalibrationPage(QWidget *parent = 0);
    QProgressBar *pBar;

    // current sensor number
    int calibrateSensorNumber;

    // average values for saving
    int avg_count;
    double average;

    double calibrate_lower;
    double calibrate_upper;

    double ref_mbar_lower;
    double ref_mbar_upper;

    // is current calibration done
    bool calibrationExceedMax;
    bool lowerCalibrationDone;


    bool upperCalibrationDone;
    bool isRunning;
    bool hasFinished;

    //switch to user action ask for ref
    bool userInputMarker;

    void setConfig(Config *conf);

    const static double DELTA = 1.0e-6;
    // calibration should not exceed 30 seconds (10ms *100 values)
    const static int MAX_AVG_COUNT = 300;

    bool showAskKeepAbortCalibrationDialog(bool *ok);
    double showAskRefMbarDialog(bool *ok);

private:
    QLabel *label;
    Config *config;

    QMetaObject::Connection connection;

    // has user canceld operation or has anything horrible happend then this is false
    bool ok;

    BimosSensorPressure *sensor;
    QMapIterator<int, BimosSensorPressure*> *sensor_iterator;


    void initializePage();
    bool isComplete() const;
    void cleanupPage();


    void setTitleInfo(int sensorNumber, double targetValue, bool increase);
    void setProgressBarColor(float value, bool isValid);
    void abortCalibration(bool complete);
    void calculateCalibration(BimosSensorPressure *sensor);
    bool calibrate(double value);

public slots:
    void newPressure(BimosSensorPressure*);


};



/** **************************************************************** */
class Calibration : public QWizard
{
    Q_OBJECT
public:
    Calibration(QWidget *parent = 0);
    CalibrationPage *calPage;
    IntroCalPage *introPage;
    Config *config;




    void setConfig(Config *conf);
    void setConnectorValue(int conNumber, float value);
    void accept();
};
/** **************************************************************** */



#endif // WIZARDCALIBRATION_H
